#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QMessageBox>
#include <QSettings>
#include <QDir>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QSoundEffect>
#include <config.h>
#include <globalhook.h>
#include <keythread.h>
#include <screenshot.h>
#include <pickpoint.h>
#include <detectwords.h>

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

protected:
    void keyPressEvent(QKeyEvent *k);
    void closeEvent(QCloseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    // void hideEvent(QHideEvent *event);


private:
    QString formatState(int state);
    void initFunctionKeys();
    void initListJP();
    void initSubjects();
    void initOneSubject(QString question, QString answer);
    void loadConfiguration();
    void loadExamConfiguration();
    void initTray();
    void getOcrAccessToken();
    void leftClick(QPoint point);
    void completeSubject();
    QString chineseToJP(QString &chinese);

private slots:
    void changeTab(int index);
    void start();
    void pause();
    void stop();
    void selectStartKey(int index);
    void selectPauseKey(int index);
    void selectStopKey(int index);
    void tryBindingWindow();
    void iconActivated(QSystemTrayIcon::ActivationReason ireason);
    void onKeyEventReceived(int vkCode);
    void onMouseEventReceived(int x, int y, QString button);
    void selectQuitMode(int index);
    void toggleSoundSwitch(bool state);
    bool validKeyValue(Qt::Key key);
    void toggleGeneralSwitch1(bool state);
    void setGeneralKey1();
    void setGeneralInterval1();
    void toggleGeneralSwitch2(bool state);
    void setGeneralKey2();
    void setGeneralInterval2();
    void toggleGeneralSwitch3(bool state);
    void setGeneralKey3();
    void setGeneralInterval3();
    void toggleGeneralSwitch4(bool state);
    void setGeneralKey4();
    void setGeneralInterval4();
    void selectSubjectRect();
    void selectOptionRect();
    void pickConfirmation();
    void pickSubmission();
    void saveRectPosition(QRect rect, int index);
    void saveOptionPoint(QPoint point, int index);

private:
    Ui::Widget *ui;
    Config *config;
    int currentTab;
    int startKey;
    int pauseKey;
    int stopKey;
    bool initializing;
    int state;
    int quitMode = 0;
    bool soundSwitch = false;
    QMap<int, QString> functionKeys;
    QStringList listJP;
    QMenu *trayMenu;
    QSystemTrayIcon *tray;
    QSoundEffect *soundEffect;
    GlobalHook hook;
    WId targetWindowId = 0;
    QScreen *screen;

    bool generalSwitch1 = false;
    int generalKey1 = 0;
    int generalInterval1 = 10;
    KeyThread *generalThread1;
    bool generalSwitch2 = false;
    int generalKey2 = 0;
    int generalInterval2 = 10;
    KeyThread *generalThread2;
    bool generalSwitch3 = false;
    int generalKey3 = 0;
    int generalInterval3 = 10;
    KeyThread *generalThread3;
    bool generalSwitch4 = false;
    int generalKey4 = 0;
    int generalInterval4 = 10;
    KeyThread *generalThread4;

    QString accessToken;
    DetectWords *detectWords;

    QPoint subjectStartPoint, subjectEndPoint;
    QPoint optionStartPoint, optionEndPoint;
    ScreenShot *screenShot;
    Config *subjects;

    QPoint confirmationPoint;;
    QPoint submissionPoint;
    PickPoint *pickPoint;


};
#endif // WIDGET_H
